package com.ruoyi.notice.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.notice.mapper.NoticeUserMapper;
import com.ruoyi.notice.domain.NoticeUser;
import com.ruoyi.notice.service.INoticeUserService;
import com.basic.service.impl.BaseServiceImpl;

/**
 * 通知的用户Service业务层处理
 * 
 * @author 王强
 * @date 2024-01-22
 */
@Service
public class NoticeUserServiceImpl extends BaseServiceImpl<Long, NoticeUser> implements INoticeUserService 
{
    public NoticeUserServiceImpl(NoticeUserMapper baseMapper) {
		super(baseMapper);
		// TODO Auto-generated constructor stub
	}
    /**
     * 查询通知的用户
     * 
     * @param id 通知的用户主键
     * @return 通知的用户
     */
    @Override
    public NoticeUser selectById(Integer id)
    {
        return baseMapper.selectById(id);
    }

    /**
     * 查询通知的用户列表
     * 
     * @param noticeUser 通知的用户
     * @return 通知的用户
     */
    @Override
    public List<NoticeUser> selectList(NoticeUser noticeUser)
    {
        return baseMapper.selectList(noticeUser);
    }

    /**
     * 新增通知的用户
     * 
     * @param noticeUser 通知的用户
     * @return 结果
     */
    @Override
    @Transactional(propagation = Propagation.MANDATORY)
    public int insert(NoticeUser noticeUser)
    {
        baseMapper.insert(noticeUser);
		return noticeUser.getId().intValue();
    }

    /**
     * 修改通知的用户
     * 
     * @param noticeUser 通知的用户
     * @return 结果
     */
    @Override
    public int update(NoticeUser noticeUser)
    {
        return baseMapper.update(noticeUser);
    }

    /**
     * 批量删除通知的用户
     * 
     * @param ids 需要删除的通知的用户主键
     * @return 结果
     */
    @Override
    public int deleteByIds(Integer[] ids)
    {
        return baseMapper.deleteByIds(ids);
    }

    /**
     * 删除通知的用户信息
     * 
     * @param id 通知的用户主键
     * @return 结果
     */
    @Override
    public int deleteById(Integer id)
    {
        return baseMapper.deleteById(id);
    }
    @Transactional(propagation = Propagation.MANDATORY)
	@Override
	public void insertNoticeUser(int[] userIds, int noticeId) {
		
    	if (null == userIds || 0 == userIds.length) {
    		
            return;
    	}
		NoticeUser[] users = parseNoticeUser(userIds, noticeId);
		
		this.baseMapper.batchInsert(users);
		
	}
	private NoticeUser[] parseNoticeUser(int[] userIds, int noticeId) {
		NoticeUser noticeUser;
		
		NoticeUser[] users = new NoticeUser[userIds.length];
		
		for(int i=0; i < userIds.length; i ++) {
			noticeUser = new NoticeUser();
            noticeUser.setNoticeId((long) noticeId);
            noticeUser.setUserId((long) userIds[i]);
            users[i] = noticeUser;
		}
		return users;
	}
	@Override
    @Transactional(propagation = Propagation.MANDATORY)
	public void updateNoticeUser(int[] userIds, int noticeId) {
		
		this.deleteById(noticeId);
		
		if (null == userIds || 0 == userIds.length) {
			return;
		}
		
		NoticeUser[] users = parseNoticeUser(userIds, noticeId);
		
		this.baseMapper.batchInsert(users);
		
	}
}
